//
//  SplitViewDelegate.m
//  MasterDetail
//
//  Created by Andreas Katzian on 15.05.10.
//  Copyright 2010 Blackwhale GmbH. All rights reserved.
//
#import "AppDelegate.h" 
#import "SplitViewDelegate.h"
#import "CustomUISplitViewController.h"

@implementation SplitViewDelegate
@synthesize delgete,showmasterUIBarButtonItem  ;
@synthesize detailController, toolBar,MasterController,AppleDeglate,popController ;

#pragma mark -
#pragma mark  分割視圖委派事件 
// the master view controller will be displayed in a popover
 - (void)splitViewController:(CustomUISplitViewController*)svc 
		  popoverController:(UIPopoverController*)pc 
  willPresentViewController:(UIViewController *)aViewController {
     NSLog(@"the master view controller will be displayed in a popover");
	 
	
}

//the master view controller will be hidden
- (void)splitViewController:(CustomUISplitViewController*)svc 
	 willHideViewController:(UIViewController *)aViewController 
		  withBarButtonItem:(UIBarButtonItem*)barButtonItem 
	   forPopoverController:(UIPopoverController*)pc {
	
    NSLog(@"the master view controller will be hidden_start");
    //當主視圖消失
    if(toolBar != nil) { 
        //infobutton
        if (infoButton==nil) {
            infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];        
            [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
            info = [[UIBarButtonItem alloc] initWithCustomView:infoButton];        
        }
        //分段區隔
        if (segmentedControl==nil) {
            segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                [NSArray arrayWithObjects:
                                 @"Day",
                                 @"Week",
                                 @"Moonth",
                                 nil]];
            
            segmentedControl.selectedSegmentIndex=0;        
            [segmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
            segmentedControl.frame = CGRectMake(0, 0, 230, 30.0);
        }
       
        if (segmentBarItem==nil) {
            segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
        }
        
         [toolBar setItems:[NSArray arrayWithObjects:showmasterUIBarButtonItem,segmentBarItem,info ,nil] animated:YES];       
     
        
        
        
        
    }
     NSLog(@"the master view controller will be hidden_end");    
     
}

//the master view will be shown again
- (void)splitViewController:(CustomUISplitViewController*)svc 
	 willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)button {
	
       NSLog(@"the master view will be shown again_start");
    
    
      [popController dismissPopoverAnimated:YES];    
    
    if(self.toolBar != nil){
         //create a toolbar
        //將 item清空
        [toolBar setItems:nil];
       
        
         
            infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];        
            [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
         
            info = [[UIBarButtonItem alloc] initWithCustomView:infoButton];        
                 
        
        //分段區隔
        segmentedControl = [[UISegmentedControl alloc] initWithItems:
                            [NSArray arrayWithObjects:
                             @"Day",
                             @"Week",
                             @"Moonth",
                             nil]];
        
        segmentedControl.selectedSegmentIndex=0;        
        [segmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.frame = CGRectMake(0, 0, 230, 30.0);
         segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
        
        
        [toolBar setItems:[NSArray arrayWithObjects: segmentBarItem ,info,nil] animated:YES]; 
        [segmentedControl release];        
        [infoButton release];        
    }
    
     NSLog(@"the master view will be shown again_start_end");    
}

#pragma mark -
#pragma mark Toobar觸動事件 
//切換日週月線 方法
-(void)segmentChanged:(id)sender{
	if (segmentedControl==sender) {
		          
		if (segmentedControl.selectedSegmentIndex==0 ) {
                 
            
            [self.delgete segmentChanged_Day];
            
            
		}
		else if(segmentedControl.selectedSegmentIndex==1){
             
            
            [self.delgete segmentChanged_Week];
            
		}
		else if(segmentedControl.selectedSegmentIndex==2){
                          
            [self.delgete segmentChanged_Month];			 	
		}		
		        
	}
}
//顯示詳細資料
-(void)showInfo{
    [self.delgete SplitViewDelegate_Delgete_showInfo:info];
}

#pragma mark -
#pragma mark  顯示主圖
-(void)init_popController{
    
    if (popController==nil)  {
        popController = [[UIPopoverController alloc] initWithContentViewController:MasterController];
        
        
    }

}


-(void)showMaster{
     //如果點選次圖 則將垂直顯示的走勢圖設定的浮動圖銷失
    if( [(AppDelegate * )AppleDeglate popController_info]!=nil ){
        [[(AppDelegate * )AppleDeglate popController_info]  dismissPopoverAnimated:YES];       
        
    }    
    if( [(AppDelegate * )AppleDeglate popController_info_next]!=nil ){
        [[(AppDelegate * )AppleDeglate popController_info_next]  dismissPopoverAnimated:YES];       
        
    }    
    
    
    
    if (popController==nil)  {
         popController = [[UIPopoverController alloc] initWithContentViewController:MasterController];
         
        
    }
    
    
    
    
    popController.popoverContentSize = CGSizeMake(320,700);
    
    [popController presentPopoverFromBarButtonItem:showmasterUIBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; }

#pragma mark -
#pragma mark  ToolBar事件
-(void)init_toolbar{
	//set title of master button
    
    NSLog(@"init_toolbar");
    //create a toolbar
    if(toolBar == nil) {
        //create a toolbar
        toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44)];
        
        
        showmasterUIBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Show Controller",@"Show Controller") style:UIBarButtonItemStyleBordered target:self action:@selector(showMaster)];         
        
        [toolBar setItems:[NSArray arrayWithObjects:showmasterUIBarButtonItem,nil] animated:YES];        
        
        
	}
    
    [detailController.view addSubview:toolBar];    
    
}
-(void)add_toolbar_item{
    NSLog(@"-(void)add_toolbar_item");
    
    if (segmentedControl==nil) {
            
        //infobutton
        infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];        
        [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
        info = [[UIBarButtonItem alloc] initWithCustomView:infoButton];        
        
        
        //分段區隔
        segmentedControl = [[UISegmentedControl alloc] initWithItems:
                            [NSArray arrayWithObjects:
                             @"Day",
                             @"Week",
                             @"Moonth",
                             nil]];
        
        segmentedControl.selectedSegmentIndex=0;        
        [segmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
        segmentedControl.frame = CGRectMake(0, 0, 230, 30.0);
         segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
        
        
        
        
        if ( [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString:@"UIInterfaceOrientationPortrait 垂直"] || [[(AppDelegate * )AppleDeglate interfaceOrientation_ipad] isEqualToString: @"UIInterfaceOrientationPortraitUpsideDown 垂直"] ) {
            
                //垂直
               
            [toolBar setItems:[NSArray arrayWithObjects:showmasterUIBarButtonItem, segmentBarItem,info ,nil] animated:YES];                 
            
            
            
            
        }
        else{
            //水平
            [toolBar setItems:[NSArray arrayWithObjects: segmentBarItem ,info,nil] animated:YES]; 
            
            
        }
        
        
        [segmentedControl release];        
        [infoButton release];  
        
        
    }
    
    
    
    
}


@end
